﻿using Bilibili.Common.Log;
using Bilibili.Model.Common;
using Bilibili.WebClient.Api;
using System;

namespace Avalonia.BiliBili.Libs.Medias
{
    public sealed class PlayerUrlClient : PlayUrlClient
    {
        protected override ReturnPlayModel AnalyzeDashResources(string re, int qn, DataSourceHeader headers)
        {
            try
            {
                using MediaSourceAdapter mediaSourceAdapter = new MediaSourceAdapter(VlcPlayer.LibVlc);
                if (mediaSourceAdapter.Analyze(re, qn))
                {
                    mediaSourceAdapter.Source.SetHeader(headers);
                    var model = BuildPlaySuccessResult(mediaSourceAdapter.MediaUrls, qn, headers, mediaSourceAdapter.Source);
                    model.VideoSize = (mediaSourceAdapter.Heigth, mediaSourceAdapter.Width);
                    return model;
                }
                else
                    return BuildPlayFailedResult("解析资源失败!");
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("AnalyzeDashResources", ex);
                return BuildPlayFailedResult(ex.Message);
            }
        }
    }
}